Skip to content

Fix sha512 buffer copy#333

Open
padelsbach wants to merge 1 commit intowolfSSL:mainfrom
padelsbach:sha512-alt-modes
Open

Fix sha512 buffer copy#333
padelsbach wants to merge 1 commit intowolfSSL:mainfrom
padelsbach:sha512-alt-modes

Conversation

@padelsbach
Copy link
Copy Markdown
Contributor

@padelsbach padelsbach commented Apr 10, 2026

Fixes F-2006 and F-2007. See related but independent wolfssl PR.

wh_Client_Sha512 unconditionally copied WC_SHA512_DIGEST_SIZE (64) bytes on finalize, regardless of SHA512 variant. For SHA512/224 (28-byte digest) and SHA512/256 (32-byte digest), this overwrites caller memory past the output buffer.

Today this is masked because the cryptocb dispatcher only handled WC_HASH_TYPE_SHA512, so variants fell through to CRYPTOCB_UNAVAILABLE and wolfSSL's software fallback handled the truncation safely. But the variants were never HSM-accelerated, and any future dispatcher fix would expose the overflow.

Changes

  • Dispatcher (wh_client_cryptocb.c): Add WC_HASH_TYPE_SHA512_224 and WC_HASH_TYPE_SHA512_256 cases, routing to the same wh_Client_Sha512/wh_Client_Sha512Dma functions with info->hash.type passed through
  • wh_Client_Sha512 (wh_client_crypto.c): New hashType parameter selects the correct digest size for memcpy and the correct wc_InitSha512*_ex variant for re-init
  • wh_Client_Sha512Dma: Same treatment -- hashType drives digestSz, req->output.sz, and DMA post-processing size
  • Tests (wh_test_crypto.c): New test cases for SHA512/224 and SHA512/256 with canary bytes to detect overwrite

Design note

The hashType parameter comes from info->hash.type, which is set by wolfSSL's cryptocb dispatch layer based on digestSz -- not from sha512->hashType, which depends on the wolfSSL port's init code setting it. This makes the fix independent of wolfSSL port behavior.

Copy link
Copy Markdown

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #333

Scan targets checked: wolfhsm-consttime, wolfhsm-crypto-bugs, wolfhsm-defaults, wolfhsm-mutation, wolfhsm-proptest, wolfhsm-src, wolfhsm-zeroize

No new issues found in the changed files. ✅

@padelsbach padelsbach marked this pull request as ready for review April 13, 2026 18:10
@rizlik rizlik assigned rizlik and unassigned wolfSSL-Bot Apr 14, 2026
@rizlik rizlik self-requested a review April 14, 2026 09:08
Copy link
Copy Markdown
Contributor

@rizlik rizlik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per PR description this was a false positive, but it's probably a better way to handle after the fixes.
Also, will hastype still needed after this PR?
A note:

  • the fact that variants were never HSM-accellerated is a false claim, as the server port looks into the hashType and do accelleration if needed.

The fact that tests do not catch the bug because of the fallback mechanism is sad, we should probably address this in wolfHSM (@bigbrett ).

* WC_HASH_TYPE_SHA512_224, or WC_HASH_TYPE_SHA512_256).
* @return int Returns 0 on success or a negative error code on failure.
*/
int wh_Client_Sha512(whClientContext* ctx, wc_Sha512* sha, const uint8_t* in,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an API break. I would prefer to add new specific function wh_Client_Sha512_224/wh_Client_Sha512_384

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

/* Setup generic header and get pointer to request data */
req = (whMessageCrypto_Sha2DmaRequest*)_createCryptoRequest(
dataPtr, WC_HASH_TYPE_SHA512, ctx->cryptoAffinity);
dataPtr, hashType, ctx->cryptoAffinity);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neither wh_Server_HandleCryptoDmaRequest nor wh_Server_HandleCryptoRequest hanlde SHA512 length variants (

wolfHSM/src/wh_server_crypto.c

Lines 4700 to 4711 in 752ca63

#if defined(WOLFSSL_SHA512)
case WC_HASH_TYPE_SHA512:
WH_DEBUG_SERVER("SHA512 req recv. type:%u\n",
rqstHeader.algoType);
ret = _HandleSha512(ctx, magic, devId, cryptoDataIn,
cryptoInSize, cryptoDataOut,
&cryptoOutSize);
if (ret != 0) {
WH_DEBUG_SERVER("SHA512 ret = %d\n", ret);
}
break;
#endif /* WOLFSSL_SHA512 */
).
This request is never handled by the server.

Test fallback to client side, that's the reason tests are not failing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed

}

#if !defined(WOLFSSL_NOSHA512_224)
static int whTest_CryptoSha512_224(whClientContext* ctx, int devId,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a multiblock test is missing

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

wc_Sha512 sha512[1];
/* Buffer sized for the variant, plus canary to detect overwrite */
uint8_t out[WC_SHA512_224_DIGEST_SIZE + 8];
const uint8_t canary = 0xA5;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason why to rely on canary and not ASAN?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt the programmatic approach was more direct. Can change if you feel strongly

@rizlik rizlik assigned padelsbach and unassigned rizlik Apr 14, 2026
@padelsbach padelsbach assigned rizlik and unassigned padelsbach Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants